module.exports = Spider;
var request = require('request'),
    async = require('async');
const sleep = (timeountMS) => new Promise((resolve) => {
    setTimeout(resolve, timeountMS);
});

function Spider(paramsList = [], limit = 1, timer = 0) {
    this.paramsList = paramsList;
    this.limit = limit;
    this.timer = timer;
}
Spider.prototype = {
    run(deal, cb) {
        var that = this;
        async.mapLimit(this.paramsList, this.limit, function(param, callback) {
            requestTo(param, callback)
        }, function(err, results) {
            // 返回最终结果
            if (err) {
                throw err;
            }
            cb(results);
        });
        async function requestTo(param, callback) {
            await sleep(that.timer);
            console.log(param.url || param + '处理请求需要' + that.timer + '毫秒')
            request(param, function(err, body, data) {
                // 数据处理
                deal(err, body, data, callback);
            })
        }
    }
};